package com.timmy.smarttest.callback;

import android.util.Log;

import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.utils.ByteUtils;
import com.timmy.smarttest.utis.DataUtils;

import java.text.DecimalFormat;

/**
 * @Auther: Timmy
 * @Data: 2017/7/4
 * @Fuction:
 */

public class BleCallbackImpl {
    public void onSearchStarted() {

    }

    public void onScan(SearchResult device) {

    }

    public void onScanStop() {

    }

    public void onBleConnected() {

    }

    public void onBleDisconnected() {

    }

    public void onNotify(byte[] value) {
        Log.i("蓝牙数据", "onNotify: " + ByteUtils.byteToString(value));

        // 分离数据


        switch (value[1]) {
            case (byte) 0x41:
                // 温湿度紫外线
                if (value[2] == 01) {
                    onEnMeasureDataRecieved(DataUtils.getIntByByte(value[3]), (DataUtils.getIntByByte(value[4]) * 256 + DataUtils.getIntByByte(value[5])) / 10, DataUtils.getIntByByte(value[6]), DataUtils.getIntByByte(value[7]));
                }
                break;
            case (byte) 0x40:
                // 心率
                if (value[2] == 01) {
                    onHeartRateRecieved(DataUtils.getIntByByte(value[5]));
                }
                break;
            case (byte) 0x90:
                // 陀螺仪
                onGyroscopeRecieved(value);

                break;
            case (byte) 0x54:
                // voc
                //onVOCMeasureData(jiaquan, tvoc, co2);
                onVOCMeasureData(value[3]);
                break;
        }
    }

    /**
     * @param tempState 温度的正 负      0 正数  1负数
     * @param temp      温度值
     * @param shidu     湿度
     * @param ziwaixian 紫外线
     */
    protected void onEnMeasureDataRecieved(int tempState, int temp, int shidu, int ziwaixian) {
    }

    /**
     * @param currentHeartRate 当前心率
     */
    protected void onHeartRateRecieved(int currentHeartRate) {
    }

    /**
     * @param jiaquan 甲醛
     * @param tvoc    tvoc
     * @param co2     co2
     */
    public void onVOCMeasureData(float jiaquan, int tvoc, int co2) {

    }

    public float formatFloat3Tofloat(double time) {
        // 格式化小数的位数
        String pattern = "0.00#";
        DecimalFormat df = new DecimalFormat(pattern);
        return Float.parseFloat(df.format(time));
    }

    // 陀螺仪数据
    private void onGyroscopeRecieved(byte[] value) {
        /***
         * 1 静止一秒
         *         90 00 000 000 00 0 00
         *
         *  等待   90  01 01 00 000
         * 2. 显示 请晃动设备
         *
         * 等待 a9 90 01 01 01 00 00 0
         *
         * 3.1 显示设备正常
         *
         * 3.2     90 01 00 00 00
         *  设备不正常
         *
         *
         */


        onGyroscope(value[2],value[3],value[4]);
    }

    public void onVOCMeasureData(byte b) {

    }

    public void onGyroscope(byte data1, byte flag, byte sign) {

    }

    private int signData(byte data1, byte data2) {

        int acc;

        if (data1 == 0xFF) {
            acc = data1 * 256 + data2 - 65536;
        } else {
            acc = data1 * 256 + data2;
        }

        return acc;
    }
}
